#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

inline void gi(int &x)
{
	x=0;char ch=getchar();
	while(ch<'0'||'9'<ch) ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
}

const int N=2e5+9;
int n,Q,c,a[N],ans[N];
vector<pair<int,int> > qry[N];

vector<int> to[N];

int fa[22][N],st[22][N],dep[N];
void dfs0(int u,int f)
{
	fa[0][u]=f;
	st[0][u]=(a[u]|a[f]);
	dep[u]=dep[f]+1;
//	cerr<<"dfs0 "<<u<<" : "<<st[0][u]<<endl;
	for(int i=1;i<=20;i++)
	{
		fa[i][u]=fa[i-1][fa[i-1][u]];
		st[i][u]=(st[i-1][u]|st[i-1][fa[i-1][u]]);
	}
	for(int v:to[u]) if(v!=f) dfs0(v,u);
}
bool chk(int u,int v,int S)
{
	if(dep[u]<dep[v]) swap(u,v);
	int T=(a[u]|a[v]);
	for(int i=20;i>=0;i--) if(dep[fa[i][u]]>=dep[v]) T|=st[i][u],u=fa[i][u];
	if(u==v) return (S&T)==T;
	for(int i=20;i>=0;i--) if(fa[i][u]!=fa[i][v]) T|=(st[i][u]|st[i][v]),u=fa[i][u],v=fa[i][v];
	T|=a[fa[0][u]];
	return (S&T)==T;
}

int vis[N];
int siz[N],mxsiz[N],rt_=0,tcnt=0;
void getrt(int u,int f)
{
	siz[u]=1,mxsiz[u]=0;
	for(int v:to[u])
	if(v!=f&&!vis[v])
	{
		getrt(v,u);
		siz[u]+=siz[v];
		mxsiz[u]=max(mxsiz[u],siz[v]);
	}
	mxsiz[u]=max(mxsiz[u],tcnt-siz[u]);
	if(mxsiz[u]<mxsiz[rt_]) rt_=u;
}
int nxt[N];
void Build(int u)
{
	vis[u]=1;
	for(int v:to[u])
	if(!vis[v])
	{
		mxsiz[rt_=0]=tcnt=siz[v];
		getrt(v,u);
		nxt[rt_]=u;
		Build(rt_);
	}
}
int B,Msk;
vector<int> tr2[N];
int buc[79][709];
int val[N],dfn[N],idfn[N],ncnt=0;
void dfs2(int u,int f)
{
	idfn[dfn[u]=++ncnt]=u;
	val[u]=(val[f]|a[u]);
	for(int v:to[u])
	if(vis[v]&&v!=f) dfs2(v,u);
}
void solve(int u)
{
	for(int v:tr2[u]) solve(v);
	vis[u]=1;
	ncnt=0;dfs2(u,0);
	for(int i=1;i<=ncnt;i++)
	{
		int ii=idfn[i];
		int P0=(val[ii]&Msk),Q=(val[ii]>>B);
		int P1=(Msk^P0);
		buc[P0][Q]++;
		for(int y=P1;y;y=((y-1)&P1)) buc[P0^y][Q]++;
	}
	for(auto pr:qry[u])
	{
		int id=pr.first,S=pr.second;
		int P=(S&Msk),Q0=(S>>B);
		ans[id]+=buc[P][0];
		for(int y=Q0;y;y=((y-1)&Q0)) ans[id]+=buc[P][y];
	}
	for(int i=1;i<=ncnt;i++)
	{
		int ii=idfn[i];
		int P0=(val[ii]&Msk),Q=(val[ii]>>B);
		int P1=(Msk^P0);
		buc[P0][Q]--;
		for(int y=P1;y;y=((y-1)&P1)) buc[P0^y][Q]--;
	}
}

signed main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	gi(n),gi(Q),gi(c);
	B=min(c,6);Msk=(1<<B)-1;
	for(int i=1;i<=n;i++) gi(a[i]),a[i]=(1<<a[i]);
	for(int i=2,f;i<=n;i++) gi(f),to[i].push_back(f),to[f].push_back(i);
	
	dfs0(1,0);
	
	mxsiz[rt_=0]=tcnt=n;
	getrt(1,0);
	Build(rt_);
	for(int i=1,u,S;i<=Q;i++)
	{
		gi(S),gi(u);
		if(!(S&a[u])) {ans[i]=1;continue;}
		int p=u,p0=u;
		while(p)
		{
			if(chk(p,u,S)) p0=p;
			p=nxt[p];
		}
		qry[p0].push_back(make_pair(i,S));
	}
	
	for(int i=1;i<=n;i++) vis[i]=0;
	for(int i=1;i<=n;i++) if(nxt[i]) tr2[nxt[i]].push_back(i);
	for(int i=1;i<=n;i++) if(!nxt[i]) solve(i);
	
	for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
}
